/**
 * created by 小卷毛, 2020-06-19 019
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.zhiyou.aifeng.utils

import androidx.recyclerview.widget.RecyclerView
import com.zhiyou.aifeng.common.base._OnScrollListener

/**
 * 项目名称：Smart_Machine
 * 创建人：小卷毛
 * 创建时间：2020-06-19 18:26
 */
fun RecyclerView.addOnScrollChanged(
    onShow: () -> Unit = {},
    onHide: () -> Unit = {}
) {
    addOnScrollListener(
        object : _OnScrollListener() {

            private val THRESHOLD = 20
            private var mDistance = 0
            private var controlsVisible = true

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                if (mDistance > THRESHOLD && controlsVisible) {
                    controlsVisible = false
                    mDistance = 0
                    onHide()
                }
                else if (mDistance < -THRESHOLD && !controlsVisible) {
                    controlsVisible = true
                    mDistance = 0
                    onShow()
                }

                if ((controlsVisible && dy > 0) || (!controlsVisible && dy < 0)) {
                    mDistance += dy
                }
            }
        }
    )
}